#include "kdtree.hpp"

#include <iostream>
#include <fstream>
#include <ctime>

#define TAM 4

/*unsigned int get_msec(void)
{
  static struct timeval timeval, first_timeval;

  gettimeofday(&timeval, 0);

  if(first_timeval.tv_sec == 0)
  {
    first_timeval = timeval;
    return 0;
  }
  return (timeval.tv_sec - first_timeval.tv_sec) * 1000 +
         (timeval.tv_usec - first_timeval.tv_usec) / 1000;
}*/

struct node
{
  typedef int tipo;

  node(const int tam, const int *a)
  {
    v = new int[tam];

    for(int i = 0; i< tam; i++)
     v[i] = a[i];
  }

  node(const int tam, const node & x)
  {
    v = new int[tam];

    for(int i = 0; i< tam; i++)
     v[i] = x.v[i];
  }

  ~node()
  {
  }

  inline tipo operator[](size_t const N) const { return v[N]; }

  tipo *v;
};

inline double tac( node t, size_t k ) { return t[k]; }

typedef KDTree::KDTree<TAM, node, std::pointer_to_binary_function<node,size_t,double> > tree;

int main(int argc, char **argv)
{
	int v[TAM];
  //unsigned int msec, start;
  clock_t start,finish; 

	node *t;

	tree src(std::ptr_fun(tac));

	std::ifstream f;
	f.open(argv[1],std::ios::in);

  //start = get_msec();

  start = clock();

	while(!f.eof())
	{
		for(int i = 0; i < TAM; i++)
      f.read((char *)(&v[i]),sizeof(int));
		t = new node(TAM,v);
		src.insert(*t);    
	}

  finish = clock();

  //msec = get_msec() - start;

	std::cout << "Tempo de leitura de arquivo e insersão na árvore: " << /*msec/1000.08*/  (double) (finish - start) / (double) (CLOCKS_PER_SEC/1000) << "ms." << std::endl;

	return 0; 
}
